package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Ygxl;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;
import com.corpcredit.service.LookupManager;

public class YgxlForm extends BasePage implements Serializable {
    private GenericManager<Ygxl, Long> ygxlManager;
    private Ygxl ygxl = new Ygxl();
    private Long id;
	private Jbqk jbqk;
	public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}

    public void setYgxlManager(GenericManager<Ygxl, Long> ygxlManager) {
        this.ygxlManager = ygxlManager;
    }

    public Ygxl getYgxl() {
        return ygxl;
    }

    public void setYgxl(Ygxl ygxl) {
        this.ygxl = ygxl;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        ygxlManager.remove(ygxl.getId());
        addMessage("ygxl.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            ygxl = ygxlManager.get(id);
        } else {
            ygxl = new Ygxl();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (ygxl.getId() == null || ygxl.getId() == 0);
        
        ygxl.setJbqk(jbqk);
        ygxl.setUsername(getRequest().getRemoteUser());
        ygxl.setOptdate(new Date());
        ygxl.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        

        ygxlManager.save(ygxl);

        String key = (isNew) ? "ygxl.added" : "ygxl.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	  this.id=this.ygxl.getId();
              return edit();

        }
    }
} 